<template>
  <div class="app-container">
    <div class="demo-input-suffix">
      <el-input
          placeholder="用户昵称、青橙号"
          prefix-icon="el-icon-search"
          v-model="page_param.key"
          @input="getQueryList"
          style="width: 300px;margin: 5px"
      >
      </el-input>

    </div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
    </el-row>
    <el-table
        v-loading="loading"
        :data="userData.records"
        @selection-change="handleSelectionChange"
        element-loading-text="Loading" border fit highlight-current-row>
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column align="center" label="头像">
        <template v-slot="scope">
          <el-avatar :size="60" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>
      <el-table-column align="center" label="昵称">
        <template v-slot="scope">
          {{ scope.row.nickName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="青橙号">
        <template v-slot="scope">
          {{ scope.row.number }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="手机号">
        <template v-slot="scope">
          {{ scope.row.phone }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.status===9">正常</el-tag>
          <el-tag type="danger" v-if="scope.row.status===0">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template v-slot="scope">
          {{ scope.row.createTime }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新时间">
        <template v-slot="scope">
          {{ scope.row.updateTime }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <el-button v-if="scope.row.status===9" size="mini" type="danger" @click="handleDisable(scope.row)">禁用
          </el-button>
          <el-button v-if="scope.row.status===0" size="mini" type="success" @click="handleAble(scope.row)">启用
          </el-button>
          <el-button size="mini" type="success" @click="handleEdit(scope.row)">编辑
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <!--    编辑、添加对话框-->
    <el-dialog :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上传头像">
              <el-upload
                  v-model:file-list="fileList"
                  action="http://127.0.0.1:5050/upload/image"
                  list-type="picture-card"
                  :on-preview="handlePreview"
                  :on-remove="handleRemove"
                  :on-success="handleUploadSuccess"
              >
                <div style="line-height: 80px;font-size: 12px;color: #9999b3">+添加优质图</div>
              </el-upload>
              <el-dialog :visible.sync="dialog.visible">
                <img width="100%" :src="dialog.imageUrl" alt="预览图像">
              </el-dialog>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="昵称" prop="">
              <el-input
                  v-model="submitData.nickName"
                  placeholder="请输入昵称"
                  maxlength="20"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="手机号" prop="">
              <el-input
                  v-model="submitData.phone"
                  placeholder="请输入手机号"
                  maxlength="11"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      fileList: [],
      open: false,
      dialog: false,
      // 多选框选中数组
      ids: [],
      page_param: {
        key: "",
        pageNumber: 1,
        pageSize: 10,
      },
      form: {},
      userData: {},
      loading: true,
      submitData: {
        image: '',
        nickName: '',
        phone: '',
        password: '',
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    getUserList() {
      this.$api.webUser.getWebUserPage(this.page_param).then(res => {
        this.userData = res.result
        this.loading = false
        this.userData.records.forEach(item => {
          if (item.avatar) {
            //item.avatar本来应该为数组，但是为字符串，导致无法直接[0]取值，
            // JSON.parse：将json解析为字符串，但item.avatar并不为json所以有错
            item.avatar = JSON.parse(item.avatar)[0]
          }
        })
      })
    },
    handleCurrentChange(val) {
      this.page_param.pageNumber = val
      this.getUserList()
    },
    getQueryList(val) {
      this.page_param.key = val
      this.getUserList()
    },
    handleDisable(row) {
      this.$confirm('此操作将停用此用户账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.webUser.disableUser(row.id).then(res => {
          this.getUserList();
          this.$message({type: 'success', message: '操作成功!'});
        })
      }).catch(() => {
        this.$message({type: 'info', message: '已取消'});
      });
    },
    handlePreview(file) {
      this.dialog.imageUrl = file.url;
      this.dialog.visible = true;
    },
    handleRemove(file, fileList) {
      this.$api.image.removeImage(file.response.result.name).then(res => {
        this.fileList = fileList
      })
    },
    handleUploadSuccess(response, file, fileList) {
      this.fileList = fileList
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAble(row) {
      this.$confirm('此操作将启用此用户账号, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.webUser.ableUser(row.id).then(res => {
          this.getUserList();
          this.$message({type: 'success', message: '操作成功!'});
        })
      }).catch(() => {
        this.$message({type: 'info', message: '已取消'});
      });
    },
    handleEdit(row) {
      this.reset()
      this.open = true;
      this.submitData.image = row.image
      this.submitData.nickName = row.nickName
      this.submitData.phone = row.phone
      this.submitData.id = row.id
    },

    handleAdd() {
      this.open = true;
      this.reset()
    },
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('确认进行删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        this.$api.user.deleteUser(ids).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getUserList();
        })
      })

    },
    reset() {
      this.submitData.image = undefined;
      this.submitData.nickName = undefined;
      this.submitData.phone = undefined;
      this.submitData.password = undefined;
      this.submitData.id = undefined;
    },
    submitForm() {
      if (this.fileList.length > 0) {
        let _fileList = this.fileList.map(file => file.response.result.url);
        this.submitData.image = JSON.stringify(_fileList)
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.submitData.id !== undefined) {
            this.$api.user.updateUser(this.submitData).then(res => {
              this.getUserList()
              this.open = false
              this.$message({type: 'success', message: '修改成功!'});

            })
          } else {
            this.$api.user.createUser(this.submitData).then(res => {
              this.getUserList()
              this.open = false
              this.$message({type: 'success', message: '创建成功!'});

            })
          }
          this.reset()
        }
      })
    },

    cancel() {
      this.open = false;
      this.reset()
    },
  },
}

</script>

